package com.kingyun.gpsinspection.purificationservice.services.jsm.innerservice.impl.station;

import com.cqhilink.api.common.utils.UniqueUtil;
import com.cqhilink.api.common.utils.date.DateStyle;
import com.cqhilink.api.common.utils.date.DateUtil;
import com.kingyun.gpsinspection.purificationservice.common.utils.CommonUtil;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.jsm.StationMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.Employee;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.Organization;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.Station;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.organization.JsmOrganizationService;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.station.JsmStationService;
import com.kingyun.gpsinspection.purificationservice.services.jsm.innerservice.util.OrgLevelUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Author 卢曼成
 * @Description 场站接口实现类
 * @Create 2017-03-09 14:55
 */

public class JsmStationServiceImpl implements JsmStationService {
    @Autowired
    private StationMapper stationMapper;
    @Autowired
    private JsmOrganizationService organizationService;

    /**
     * @Author 卢曼成
     * @Description 根据ID物理删除
     * @Date 2017/3/9 14:57
     */
    @Override
    public int deleteByPrimaryKey(String stationId) {
        return stationMapper.deleteByPrimaryKey(stationId);
    }

    /**
     * @Author 卢曼成
     * @Description 新增信息
     * @Date 2017/3/9 14:57
     */
    @Override
    public int insert(Station record) {
        return stationMapper.insert(record);
    }

    /**
     * @Author 卢曼成
     * @Description 判断实体类信息不为空新增
     * @Date 2017/3/9 14:57
     */
    @Override
    public int insertSelective(Station record) {
        return stationMapper.insertSelective(record);
    }

    /**
     * @Author 卢曼成
     * @Description 根据ID查询
     * @Date 2017/3/9 14:59
     */
    @Override
    public Station selectByPrimaryKey(String stationId) {
        return stationMapper.selectByPrimaryKey(stationId);
    }

    /**
     * @Author 卢曼成
     * @Description 根据ID，判断实体类信息不为空修改
     * @Date 2017/3/9 14:59
     */
    @Override
    public int updateByPrimaryKeySelective(Station record) {
        return stationMapper.updateByPrimaryKeySelective(record);
    }

    /**
     * @Author 卢曼成
     * @Description 根据ID修改
     * @Date 2017/3/9 15:00
     */
    @Override
    public int updateByPrimaryKey(Station record) {
        return stationMapper.updateByPrimaryKey(record);
    }

    /**
     * @Author 卢曼成
     * @Description 根据ID逻辑删除
     * @Date 2017/3/9 15:00
     */
    @Override
    public int updateStatus(String stationId) {
        return stationMapper.updateStatus(stationId);
    }

    /**
     * @Author 卢曼成
     * @Description 根据条件查询集合
     * @Date 2017/3/9 15:23
     */
    @Override
    public List<Station> selectByPrimaryList(Station record) {
        return stationMapper.selectByPrimaryList(record);
    }

    /**
     * @Author 卢曼成
     * @Description 添加修改
     * @Date 2017/3/9 15:23
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int saveOrUpdate(Station record, Employee employee) throws Exception {
        String userId = employee.getEmployeeId();
        Date date = new Date();
        int num;
        record.setBsflag("0");
        Station station = stationMapper.selectByPrimaryKey(record.getOrgId());
        String orgId = null;
        Organization org = new Organization();
    /*  if (station != null) {
            record.setOrgId(station.getOrgId());
            record.setParentId(station.getStationId());
            org.setParentOrgId(station.getStationId());
        } else {
            Organization orgs = organizationService.findOrgById(record.getOrgId());
            if (orgs != null) {
                record.setOrgId(orgs.getOrgId());
                org.setParentOrgId(orgs.getOrgId());
            } else {
                throw new Exception("场站上级组织错误");
            }
        }*/
        org.setOrgName(record.getStationName());
        if (StringUtils.isNotEmpty(record.getStationId())) {
            org.setOrgId(record.getStationId());
            org.setOperateId(userId);
            org.setOperateDT(DateUtil.DateToString(date, DateStyle.YYYY_MM_DD_HH_MM));
            //场站数据
            record.setUpdateDate(date);
            record.setUpdateUserId(userId);
            record.setCheckDate(new Date());
            organizationService.updateByPrimaryKeySelective(org);
            num = stationMapper.updateByPrimaryKeySelective(record);
        } else {
            Organization orgs = organizationService.findOrgById(record.getOrgId());
            if (orgs != null) {
                record.setOrgId(orgs.getOrgId());
                org.setParentOrgId(orgs.getOrgId());
            }
            String id = UniqueUtil.uuid();
            org.setOrgId(id);
            org.setCreateDT(DateUtil.DateToString(date, DateStyle.YYYY_MM_DD_HH_MM));
            org.setCreatorId(userId);
            org.setOrgLevelId(OrgLevelUtils.ORG_LEVEL_STATION);
            org.setOperateDT(CommonUtil.getTimeString());
            record.setCreateDate(date);
            record.setCreateUserId(userId);
            record.setStationId(id);
            record.setUpdateDate(new Date());
            record.setUpdateUserId(userId);
            record.setCheckDate(new Date());
            organizationService.insert(org);
            num = stationMapper.insert(record);
        }
        return num;
    }

    /**
     * @param record
     * @return
     * @Author 卢曼成
     * @Description 根据条件查询树形
     */
    @Override
    public List<Station> selectByTree(Station record) {
        return stationMapper.selectByTree(record);
    }

    /**
     * @param record
     * @return
     * @Author 卢曼成
     * @Description 根据条件查询下级总数
     */
    @Override
    public List<Station> selectByItem(Station record) {
        return stationMapper.selectByItem(record);
    }

    /**
     * @param list
     * @Author 卢曼成
     * @Description 批量添加
     * @Date 2017/2/8 17:17
     */
    @Override
    public int insertList(List<Station> list) {
        return stationMapper.insertList(list);
    }


    /**
     * @param list
     * @Author 卢曼成
     * @Description 批量修改
     * @Date 2017/2/8 17:17
     */
    @Override
    public int updateList(List<Station> list) {
        return stationMapper.updateList(list);
    }

    /**
     * @param list
     * @Author 卢曼成
     * @Description 批量逻辑删除
     * @Date 2017/2/8 17:18
     */
    @Override
    public int deleteList(List<Station> list) {
        return stationMapper.deleteList(list);
    }
}
